package RFAdapter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import br.unicamp.ic.lis.gp.strategies.GPObjectsSelector;
import br.unicamp.ic.lis.gpplus.GPPlusRelevanceLevel;
import br.unicamp.ic.lis.gpplus.config.ConfigManager;
import br.unicamp.ic.lis.rfframework.impl.interfaces.IUserActionListener;
import br.unicamp.ic.lis.rfframework.objects.DigitalObject;
import br.unicamp.ic.lis.rfframework.objects.DigitalObjectSet;
import br.unicamp.ic.lis.rfframework.objects.LabeledDigitalObject;
import br.unicamp.ic.lis.rfframework.objects.exceptions.EmptyCollectionException;
import br.unicamp.ic.lis.rfframework.objects.exceptions.ObjectNotFoundException;

public class GUIUserHandler implements IUserActionListener{

	private int current_iteration;
	private int maxNumberOfIterations;
        boolean rel[];

	public GUIUserHandler(){
		current_iteration = 0;
		maxNumberOfIterations = ConfigManager.getInstance().getMaxFeedbackIterations() - 1;
	}

	public DigitalObjectSet<LabeledDigitalObject> indicationOfQueryPattern(DigitalObjectSet<DigitalObject> collection) {

            DigitalObjectSet<LabeledDigitalObject> queryPattern = new DigitalObjectSet<LabeledDigitalObject>();
            System.out.print("\nIndique ids do padrão de consulta: ");
            Properties prop = new Properties();            
            int numberOfObjects;

            //escrever resources/userQueryPattern.properties
            // criar metodoq ue chame o que vem..
                   /////--> prop.setProperty("QP[1]", );
            try {
                            prop.load(new FileInputStream("resources/userQueryPattern.properties"));
                            numberOfObjects = Integer.parseInt(prop.getProperty("NUMBER_OF_OBJECTS"));

                    for(int i = 0; i < numberOfObjects; i++){
                                    DigitalObject digitalObject = null;
                                    String id = prop.getProperty("QP[" + i + "]");
                                    try {    digitalObject = (DigitalObject) collection.getObjectById((Integer.parseInt(id)));//passar id de consultaaa
                                    } catch (Exception e) {    e.printStackTrace();   }
                                    
                                    LabeledDigitalObject labeledObject = new LabeledDigitalObject(digitalObject, (new GPPlusRelevanceLevel(GPPlusRelevanceLevel.RELEVANT)));
                                    queryPattern.addObject(labeledObject);
                                    GPObjectsSelector.objectsShown.addObject(digitalObject);
                                    ConfigManager.getInstance().getColectionAccessManager().addObject(labeledObject);
                                    System.out.print(labeledObject.getId() + " ");
                            }

                    System.out.println("\n");

            } catch (FileNotFoundException e1) {
                            e1.printStackTrace();
                    } catch (IOException e1) {
                            e1.printStackTrace();
                    }

            System.out.println();

            return queryPattern;
	}

	/*public DigitalObjectSet<LabeledDigitalObject> indicationOfQueryPatternViaTerminal(DigitalObjectSet<DigitalObject> collection) {
		DigitalObjectSet<LabeledDigitalObject> queryPattern = new DigitalObjectSet<LabeledDigitalObject>();
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    System.out.print("Indique ids do padrão de consulta: ");
	    String[] linha = null;
		try {
			linha = br.readLine().split(" ");
		} catch (IOException e) {
			e.printStackTrace();
		}

	    for(int i = 0; i < linha.length; i++){
			DigitalObject digitalObject = null;
			try {
				digitalObject = (DigitalObject) collection.getObjectById((Integer.parseInt(linha[i])));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			} catch (EmptyCollectionException e) {
				e.printStackTrace();
			}
			LabeledDigitalObject labeledObject = new LabeledDigitalObject(digitalObject, (new GPPlusRelevanceLevel(GPPlusRelevanceLevel.RELEVANT)));
			queryPattern.addObject(labeledObject);
			System.out.print(labeledObject.getId() + " ");
		}

	    System.out.println();

	    return queryPattern;
	}*/

	public boolean showSetOfObjects(DigitalObjectSet<DigitalObject> retrievedObjects) {
//o usuario decide se vai continuar
	        //this.current_iteration++;
	        return false;
	}

	public DigitalObjectSet<LabeledDigitalObject> userIndicationOfObjectsRelevance(
			DigitalObjectSet<DigitalObject> retrievedObjects, DigitalObjectSet<LabeledDigitalObject> queryPattern) {

		DigitalObjectSet<LabeledDigitalObject> labeledObjects = new DigitalObjectSet<LabeledDigitalObject>();
		GPPlusRelevanceLevel relevanceLevel;
		LabeledDigitalObject labeledObject;
		DigitalObject digitalObject;

                //exibir retrievedObjects
////////////////////////////////////////////////////////////////////////////////////////////////////
                //implementar
                /////////////////////////////////////////////////////////////////////////
                //pegar o feedback
                rel = new boolean[retrievedObjects.getSize()];

        try {

        	long queryObjectID = ((LabeledDigitalObject) queryPattern.getObjectByIndex(0)).getId();
			for(int i = 0; i < retrievedObjects.getSize(); i++){

				digitalObject = (DigitalObject) retrievedObjects.getObjectByIndex(i);

				if(isRelevant(i)){
					relevanceLevel = new GPPlusRelevanceLevel(GPPlusRelevanceLevel.RELEVANT);
				}
				else
					relevanceLevel = new GPPlusRelevanceLevel(GPPlusRelevanceLevel.UNKNOWN);

				System.out.println("id: " + digitalObject.getId() + " label: " + relevanceLevel.getValue());
				labeledObject = new LabeledDigitalObject(digitalObject, relevanceLevel);
				labeledObjects.addObject(labeledObject);
			}
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		} catch (EmptyCollectionException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		return labeledObjects;

	}

	private boolean isRelevant(int i){
		return rel[i];
	}

}
